FROM ubuntu:18.04

COPY src /home/work/software/src

ENV TZ=Asia/Shanghai \
    DEBIAN_FRONTEND=noninteractive \
    LC_ALL=zh_CN.UTF-8

RUN    cd / \
    && cp /home/work/software/src/sources.list /etc/apt/sources.list \
    && apt -y update \
    && apt -y upgrade \

    #设置时间添加用户
    && apt install -y tzdata locales\
    && locale-gen zh_CN.UTF-8 \
    && update-locale LANG=zh_CN.UTF-8 LANGUAGE=zh_CN.UTF-8 LC_ALL=zh_CN.UTF-8 \
    && ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
    && echo ${TZ} > /etc/timezone \
    && dpkg-reconfigure --frontend noninteractive tzdata \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd work -g 501 && useradd work -u 501 -g 501  -s /sbin/nologin \

    #安装依赖
    && apt install -y wget \
    && apt install -y autoconf automake m4 \
    && apt install -y vim \
    && apt install -y git \
    && apt install -y gcc \
    && apt install -y make \
    && apt install -y curl \
    && apt install -y libbz2-dev \
    && apt install -y libjpeg-dev \
    && apt install -y libpng-dev \
    && apt install -y libfreetype6-dev \
    && apt install -y libzip-dev \
    && apt install -y libssl-dev \
    && apt install -y libsqlite3-dev \
    && apt install -y libcurl4-openssl-dev \
    && apt install -y libgmp3-dev \
    && apt install -y libonig-dev \
    && apt install -y libreadline-dev \
    && apt install -y libxslt1-dev \
    && apt install -y libffi-dev \
    && apt install -y pkg-config \
    && apt install -y libldap2-dev \
    && apt install -y libgeoip-dev \
    && apt install -y libgd-dev \
    && apt install -y libxml2-dev \
    && apt install -y libpcre3-dev \
    && apt install -y libjpeg62-dev \
    && apt install -y libfreetype6-dev \
    && apt install -y libpnglite-dev \
    && apt install -y libiconv-hook-dev libiconv-hook1 \
    && apt install -y libmcrypt-dev libmcrypt4 \
    && apt install -y libmhash-dev libmhash2 \
    && apt install -y libltdl-dev libssl-dev \
    && apt install -y libcurl4-openssl-dev \
    && apt install -y libmysqlclient-dev \
    && apt install -y libmagickcore-dev libmagickwand-dev \
    && apt install -y libedit-dev \
    && ln -s /usr/lib/libiconv_hook.so.1.0.0 /usr/lib/libiconv.so \
    && ln -s /usr/lib/libiconv_hook.so.1.0.0 /usr/lib/libiconv.so.1 \

    #安装nginx
    && cd /home/work/software/src \
    && sh nginx_get_src.sh \
    && cd /home/work/software/src/luajit2 \
    && make PREFIX=/home/work/software/luajit \
    && make install PREFIX=/home/work/software/luajit \
    && echo "/home/work/software/luajit/lib" >> /etc/ld.so.conf \
    && ldconfig \
    && cd /home/work/software/src/tengine-2.3.3 \
    && patch -p1 < /home/work/software/src/ngx_req_status/write_filter-1.7.11.patch \
    && apt install -y libgd-dev \
    && ./configure \
       --prefix=/home/work/software/tengine \
       --with-http_realip_module \
       --with-http_addition_module \
       --with-http_xslt_module \
       --with-http_image_filter_module \
       --with-http_geoip_module \
       --with-http_sub_module \
       --with-http_secure_link_module \
       --with-http_degradation_module \
       --with-http_lua_module \
       --with-pcre=/home/work/software/src/pcre-8.42 \
       --with-luajit-inc=/home/work/software/luajit/include/luajit-2.1 \
       --with-luajit-lib=/home/work/software/luajit/lib \
       --add-module=/home/work/software/src/ngx_req_status \
    && make -j4 \
    && make install \
    && cp /home/work/software/src/tengine /etc/init.d \
    && chmod 775 /etc/init.d/tengine

    #安装php
    && cd /home/work/software/src \
    && sh php_get_src.sh \
    && cd /home/work/software/src/php-7.1.15 \
    && ./configure \
       --prefix=/home/work/software/php7.1 \
       --with-config-file-path=/home/work/software/php7.1/etc \
       --with-curl \
       --with-freetype-dir \
       --with-gd \
       --with-gettext \
       --with-iconv-dir \
       --with-kerberos \
       --with-libdir=lib64 \
       --with-libxml-dir \
       --with-mysqli \
       --with-openssl \
       --with-pcre-regex \
       --with-pdo-mysql \
       --with-pdo-sqlite \
       --with-pear \
       --with-png-dir \
       --with-xmlrpc \
       --with-xsl \
       --with-zlib \
       --enable-fpm \
       --enable-bcmath \
       --enable-libxml \
       --enable-inline-optimization \
       --enable-gd-native-ttf \
       --enable-mbregex \
       --enable-mbstring \
       --enable-opcache \
       --enable-pcntl \
       --enable-shmop \
       --enable-soap \
       --enable-sockets \
       --enable-sysvsem \
       --enable-xml \
       --enable-zip \
       --enable-ftp \
       --enable-calendar \
       --enable-exif \
    && make -j4 \
    && make install \
    && cp /home/work/software/src/php-7.1.15/php.ini-development /home/work/software/php7.1/etc/php.ini \
    && ln -sf /home/work/software/php7.1/bin/php /usr/bin/php \
    #安装protobuf扩展
    && cd /home/work/software/src/protobuf-3.17.3 \
    && /home/work/software/php7.1/bin/phpize \
    && ./configure --with-php-config=/home/work/software/php7.1/bin/php-config \
    && make -j4 \
    && make install \
    #安装redis扩展
    && cd /home/work/software/src/redis-5.3.4 \
    && /home/work/software/php7.1/bin/phpize \
    && ./configure --with-php-config=/home/work/software/php7.1/bin/php-config \
    && make -j4 \
    && make install \
    #安装mongodb扩展
    && cd /home/work/software/src/mongodb-1.9.1 \
    && /home/work/software/php7.1/bin/phpize \
    && ./configure --with-php-config=/home/work/software/php7.1/bin/php-config \
    && make -j4 \
    && make install \
    #安装swoole扩展
    && cd /home/work/software/src/swoole-4.4.26 \
    && /home/work/software/php7.1/bin/phpize \
    && ./configure --with-php-config=/home/work/software/php7.1/bin/php-config \
    && make -j4 \
    && make install \
    #安装librdkafka
    && cd /home/work/software/src/librdkafka \
    && ./configure \
    && make -j4 \
    && make install \
    #安装rdkafka扩展,依赖上面的librdkafka
    && cd /home/work/software/src/rdkafka-5.0.0 \
    && /home/work/software/php7.1/bin/phpize \
    && ./configure --with-php-config=/home/work/software/php7.1/bin/php-config \
    && make -j4 \
    && make install \
    #安装mcrypt扩展
    && cd /home/work/software/src/mcrypt-1.0.1 \
    && /home/work/software/php7.1/bin/phpize \
    && ./configure --with-php-config=/home/work/software/php7.1/bin/php-config \
    && make -j4 \
    && make install \
    #安装icu
    && cd /home/work/software/src/icu/source/ \
    && ./configure --prefix=/usr/local/icu \
    && make -j4 \
    && make install \
    #安装intl扩展
    && cd /home/work/software/src/php-7.1.15/ext/intl \
    && /home/work/software/php7.1/bin/phpize \
    && ./configure --enable-intl --with-icu-dir=/usr/local/icu/ --with-php-config=/home/work/software/php7.1/bin/php-config \
    && make -j4 \
    && make install \
    #安装ldap扩展
    && cd /home/work/software/src/php-7.1.15/ext/ldap \
    && /home/work/software/php7.1/bin/phpize \
    && ./configure --with-php-config=/home/work/software/php7.1/bin/php-config --with-ldap \
    && make -j4 \
    && make install \
    #安装grpc扩展(这个安装比较慢，并且比较大)
    && cd /home/work/software/src/grpc-1.38.0 \
    && /home/work/software/php7.1/bin/phpize \
    && ./configure --with-php-config=/home/work/software/php7.1/bin/php-config \
    && make -j4 \
    && make install \
    #添加扩展到配置文件
    && echo "extension=protobuf.so" >> /home/work/software/php7.1/etc/php.ini \
    && echo "extension=redis.so" >> /home/work/software/php7.1/etc/php.ini \
    && echo "extension=mongodb.so" >> /home/work/software/php7.1/etc/php.ini \
    && echo "extension=swoole.so" >> /home/work/software/php7.1/etc/php.ini \
    && echo "extension=rdkafka.so" >> /home/work/software/php7.1/etc/php.ini \
    && echo "extension=mcrypt.so" >> /home/work/software/php7.1/etc/php.ini \
    && echo "extension=intl.so" >> /home/work/software/php7.1/etc/php.ini \
    && echo "extension=ldap.so" >> /home/work/software/php7.1/etc/php.ini \
    && echo "extension=grpc.so" >> /home/work/software/php7.1/etc/php.ini \
    #配置php
    && cp /home/work/software/src/php-fpm.conf /home/work/software/php7.1/etc \
    && cp /home/work/software/src/php71-fpm /etc/init.d \
    && chmod 775 /etc/init.d/php71-fpm \
    #缩减镜像大小
    && cd /home/work/software \
    && rm -rf /home/work/software/src \
    && rm -rf /var/lib/apt/lists/* \
    && find /usr/local/ -type f -executable -exec ldd '{}' ';' | awk '/=>/ { print $(NF-1) }' | sort -u | xargs -r dpkg-query --search | cut -d: -f1 | sort -u | xargs -r apt-mark manual \
    && apt purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false